Partial Public Class SecurityTest
    Inherits System.Web.UI.Page

    Private allUsers As MembershipUserCollection = System.Web.Security.Membership.GetAllUsers
    Private allRoles As String() = System.Web.Security.Roles.GetAllRoles
    Dim dtRoles As New Data.DataTable("Roles")

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        mvSecurity.ActiveViewIndex = 0
        If Page.IsPostBack = False Then
            lblTotUsers.Text = allUsers.Count.ToString
            lblOnlineUsers.Text = System.Web.Security.Membership.GetNumberOfUsersOnline.ToString
            Dim alphabet As String() = _
                "A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z;All".Split(";")
            rptAlphabet.DataSource = alphabet
            rptAlphabet.DataBind()
            BindUsers(False)
            BindRoles(False)
        End If
    End Sub

    Private Sub BindRoles(ByVal reloadAllRoles As Boolean)
        dtRoles.Columns.Add("RoleName")

        If reloadAllRoles Then
            allRoles = System.Web.Security.Roles.GetAllRoles
        End If
        If Not allRoles Is Nothing Then
            For Each Item As String In allRoles
                Dim drNewRow As Data.DataRow = dtRoles.NewRow
                drNewRow.Item("RoleName") = Item
                dtRoles.Rows.Add(drNewRow)
            Next
        End If
        gvRoles.DataSource = dtRoles
        gvRoles.DataBind()
    End Sub
    Private Sub BindUsers(ByVal reloadAllUsers As Boolean)
        If reloadAllUsers Then
            allUsers = System.Web.Security.Membership.GetAllUsers
        End If

        Dim users As MembershipUserCollection = Nothing

        Dim searchText As String = ""
        If Not String.IsNullOrEmpty(gvwUsers.Attributes("SearchText")) Then
            searchText = gvwUsers.Attributes("SearchText")
        End If

        Dim searchByEmail As Boolean = False
        If Not String.IsNullOrEmpty(gvwUsers.Attributes("SearchByEmail")) Then
            searchByEmail = Boolean.Parse(gvwUsers.Attributes("SearchByEmail"))
        End If

        If searchText.Length > 0 Then
            If searchByEmail Then
                users = System.Web.Security.Membership.FindUsersByEmail(searchText)
            Else
                users = System.Web.Security.Membership.FindUsersByName(searchText)
            End If
        Else
            users = allUsers
        End If

        gvwUsers.DataSource = users
        gvwUsers.DataBind()
    End Sub

    Protected Sub rptAlphabet_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptAlphabet.ItemCommand
        gvwUsers.Attributes.Add("SearchByEmail", Boolean.FalseString)

        If e.CommandArgument.ToString.Length = 1 Then
            gvwUsers.Attributes.Add("SearchText", e.CommandArgument.ToString + "%")
            BindUsers(False)
        Else
            gvwUsers.Attributes.Add("SearchText", "")
            BindUsers(False)
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim searchByEmail As Boolean = (ddlSearchTypes.SelectedValue = "E-mail")
        gvwUsers.Attributes.Add("SearchText", txtSearchText.Text)
        gvwUsers.Attributes.Add("SearchByEmail", searchByEmail.ToString)
        BindUsers(False)
    End Sub

    Protected Sub gvwUsers_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvwUsers.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim btn As ImageButton = CType(e.Row.Cells(7).Controls(0), ImageButton)
            btn.OnClientClick = "if (confirm('Are you sure you want to delete this user account?') == false) return false;"
        End If
    End Sub

    Protected Sub gvwUsers_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvwUsers.RowDeleting
        Dim userName As String = gvwUsers.DataKeys(e.RowIndex).Value.ToString
        ProfileManager.DeleteProfile(userName)
        System.Web.Security.Membership.DeleteUser(userName)
        BindUsers(True)
        lblTotUsers.Text = allUsers.Count.ToString
    End Sub

    Protected Sub mnuSecurity_MenuItemClick(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles mnuSecurity.MenuItemClick
        mvSecurity.ActiveViewIndex = CType(e.Item.Value, Integer)
    End Sub

    Private Sub gvRoles_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRoles.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim btn As ImageButton = CType(e.Row.Cells(2).Controls(0), ImageButton)
            btn.OnClientClick = "if (confirm('Are you sure you want to delete this role?') == false) return false;"
        End If
    End Sub

    Private Sub gvRoles_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvRoles.RowDeleting
        Dim roleName As String = gvRoles.DataKeys(e.RowIndex).Value.ToString
        System.Web.Security.Roles.DeleteRole(roleName, True)
        BindRoles(True)
    End Sub

    Protected Sub btnAddRole_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRole.Click
        If System.Web.Security.Roles.RoleExists(txtAddRoleName.Text) = False Then
            System.Web.Security.Roles.CreateRole(txtAddRoleName.Text)
        End If
        txtAddRoleName.Text = String.Empty
        BindRoles(True)
    End Sub
End Class